﻿Imports System.Windows.Forms

Public Class ExceptionDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub


    Private CaughtException As Exception

    Public Sub New(ByVal ex As Exception)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        CaughtException = ex
    End Sub

    Public Shared Function DisplayException(ByVal ex As Exception) As DialogResult
        Dim DLG As New ExceptionDialog(ex)
        Return DLG.ShowDialog()
    End Function


    Public Shared Function DisplayException(ByVal ex As Exception, ByVal owner As IWin32Window) As DialogResult
        Dim DLG As New ExceptionDialog(ex)
        Return DLG.ShowDialog_Private(owner)
    End Function

    Private Delegate Function ShowDialog_PrivateDelegate(ByVal owner As IWin32Window) As DialogResult
    Private Function ShowDialog_Private(ByVal owner As IWin32Window) As DialogResult
        If DirectCast(owner, Form).InvokeRequired Then
            Dim D As New ShowDialog_PrivateDelegate(AddressOf ShowDialog_Private)
            Dim O() As Object = {owner}
            Return DirectCast(owner, Form).Invoke(D, O)
        Else
            Return Me.ShowDialog(owner)
        End If
    End Function


End Class
